package com.code.leetcode.center50;

import com.code.leetcode.utils.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/26 19:42
 */
public class The107 {

    ArrayList<List<Integer>> lists = new ArrayList<>();

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        helper(root, 0);
        Collections.reverse(lists);
        return lists;
    }

    private void helper(TreeNode node, int level) {
        if (node == null) return;
        if (level >= lists.size()) {
            lists.add(new ArrayList<>());
        }
        lists.get(level).add(node.val);
        if (node.left != null)
            helper(node.left, level + 1);
        if (node.right != null)

            helper(node.right, level + 1);
    }
}
